package com.utils;


import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Shader.TileMode;
import android.os.AsyncTask;
import android.widget.ImageView;


public class CircularPictureClass extends AsyncTask<String, String, Bitmap> {

    private final Bitmap bitmap;

    private onCircularCompleted complete;

    private final ImageView image;

    public interface onCircularCompleted {

        void Completed(Bitmap circular);
    }

    public CircularPictureClass(Bitmap bitmap, ImageView image) {

        super();
        this.bitmap = bitmap;
        this.image = image;
    }

    public CircularPictureClass(Bitmap bitmap, ImageView image,
                onCircularCompleted complete) {

        super();
        this.bitmap = bitmap;
        this.image = image;
        this.complete = complete;
    }

    @Override
    protected Bitmap doInBackground(String... arg0) {

        Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        //
        BitmapShader shader = new BitmapShader(bitmap, TileMode.REPEAT, TileMode.REPEAT);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setShader(shader);
        //
        Canvas c = new Canvas(circleBitmap);
        c.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
        //

        return circleBitmap;
        //
    }

    @Override
    protected void onPostExecute(Bitmap result) {

        if (complete != null)
            complete.Completed(result);
        else
            image.setImageBitmap(result);

    }
}
